package com.flyaudio.xiaopengautopilot.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyaudio.xiaopengautopilot.R;
import com.flyaudio.xiaopengautopilot.presenter.IVehicleModule;
import com.flyaudio.xiaopengautopilot.constant.MsgViewUtil;
import com.flyaudio.xiaopengautopilot.util.MySharedPreferences;

public class ChargingLimitDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private int resLayout;

    private Button vehicle_close_bt;
    private LinearLayout vehicle_daily_mode_ll, vehicle_long_distance_ll;
    private TextView vehicle_daily_mode_tx, vehicle_daily_mode_tips1_tx, vehicle_daily_mode_tips2_tx;
    private TextView vehicle_long_distance_tx, vehicle_long_distance_tips1_tx, vehicle_long_distance_tips2_tx;

    private Drawable unselectDrawable,selectDrawable;
    private int unselectColor,selectColor;

    public ChargingLimitDialog(Context context) {
        super(context);
        this.context = context;
    }

    public ChargingLimitDialog(Context context, int resLayout) {
        super(context);
        this.context = context;
        this.resLayout = resLayout;
    }

    public ChargingLimitDialog(Context context, int theme, int resLayout) {
        super(context, theme);
        this.context = context;
        this.resLayout = resLayout;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(resLayout);

        Window dialogWindow = getWindow();
        WindowManager manager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.TOP|Gravity.CENTER);

        initView();

        MySharedPreferences.attachContext(context);
        updateView();
    }

    private void initView() {
        vehicle_close_bt = (Button) findViewById(R.id.vehicle_close_bt);
        vehicle_daily_mode_ll = (LinearLayout) findViewById(R.id.vehicle_daily_mode_ll);
        vehicle_long_distance_ll = (LinearLayout) findViewById(R.id.vehicle_long_distance_ll);
        vehicle_daily_mode_tx = (TextView) findViewById(R.id.vehicle_daily_mode_tx);
        vehicle_daily_mode_tips1_tx = (TextView) findViewById(R.id.vehicle_daily_mode_tips1_tx);
        vehicle_daily_mode_tips2_tx = (TextView) findViewById(R.id.vehicle_daily_mode_tips2_tx);
        vehicle_long_distance_tx = (TextView) findViewById(R.id.vehicle_long_distance_tx);
        vehicle_long_distance_tips1_tx = (TextView) findViewById(R.id.vehicle_long_distance_tips1_tx);
        vehicle_long_distance_tips2_tx = (TextView) findViewById(R.id.vehicle_long_distance_tips2_tx);

        vehicle_daily_mode_ll.setOnClickListener(this);
        vehicle_long_distance_ll.setOnClickListener(this);
        vehicle_close_bt.setOnClickListener(this);

        unselectDrawable = context.getDrawable(R.drawable.vehicle_dialog_select_u);
        selectDrawable = context.getDrawable(R.drawable.vehicle_dialog_select_d);
        unselectColor = context.getColor(R.color.colorBlack);
        selectColor= context.getColor(R.color.colorWhite);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.vehicle_close_bt:
                dismiss();
                break;
            case R.id.vehicle_daily_mode_ll:
                IVehicleModule.getInstance().SendActivityMessage(MsgViewUtil.Update_Daily_Mode);
                setChargingModeView(selectDrawable,unselectDrawable,selectColor,unselectColor);
                break;
            case R.id.vehicle_long_distance_ll:
                IVehicleModule.getInstance().SendActivityMessage(MsgViewUtil.Update_Long_Distance_Mode);
                setChargingModeView(unselectDrawable,selectDrawable,unselectColor,selectColor);
                break;
        }
    }

    private void setChargingModeView(Drawable dailyDrawable, Drawable longDrawable, int dailyColor, int longColor) {
        vehicle_daily_mode_ll.setBackground(dailyDrawable);
        vehicle_daily_mode_tx.setTextColor(dailyColor);
        vehicle_daily_mode_tips1_tx.setTextColor(dailyColor);
        vehicle_daily_mode_tips2_tx.setTextColor(dailyColor);

        vehicle_long_distance_ll.setBackground(longDrawable);
        vehicle_long_distance_tx.setTextColor(longColor);
        vehicle_long_distance_tips1_tx.setTextColor(longColor);
        vehicle_long_distance_tips2_tx.setTextColor(longColor);
    }

    private void updateView() {
        if(MySharedPreferences.getPowerChargingLimitMode()) {
            setChargingModeView(unselectDrawable,selectDrawable,unselectColor,selectColor);
        }else {
            setChargingModeView(selectDrawable,unselectDrawable,selectColor,unselectColor);
        }
    }

}
